if(files.cwd().endsWith("脚本")){
  let _require = require;
  require = function(path){
    if(path.startsWith("./")){
      path = path.replace("./","");
    }
    path = files.cwd() + "/gdt_hb/" + path;
    return _require(path);
  }
}


auto.waitFor()
var utils = require("./utils.js");
var storage = storages.create("xiangxiangyuedu");

function launchAppEnhance(app_name) {
    let thread = threads.start(function () {
        toastLog("检查是否有打开权限!");
        while (true) {
            var allow = text("允许").findOne(5000);
            if (allow) {
                sleep(1000)
                toastLog("发现允许授权!");
                allow.click();
                sleep(1000);
                thread.interrupt();
                break;
            }
        }
    });
    sleep(1000);
    var result = launchApp(app_name);
    sleep(3000);
    thread.interrupt();
    return result;
  }

  
function task(){
    var xs = id("tabTV").className("android.widget.TextView").text("晓食").findOne(15000);
    if(xs){ 
        xs.parent().parent().click();
        sleep(2000)
        var tasks = text("去做任务").find(5000);
        log("任务length:", tasks.size());
        if(tasks.length > 0){
            // tasks[0].click();去掉发布摊训释放加成的任务
            tasks[1].click();
            sleep(3000)
            var tt = className("android.view.View").depth(10).find();
            log(tt.length)
            tt[1].click();//点击基础概念
            var 查看全文 = text("查看全文").findOne(10000);
            if(查看全文){
                查看全文.click();
                sleep(5000);
                back();
            }
            sleep(1000);
            utils.updateAppProgress(20);
            back();
            sleep(1000);
            back();
            tasks[2].click();
            sleep(1000)
            var dianzhan = className("android.widget.Image").text("icon-like").findOne(1000);
            if(dianzhan){
              dianzhan.click();
              toast("点赞任务已完成");           
            }else{
              log("没找到");
            }
            sleep(3000)
            log("回到晓食");
            xs.parent().parent().click();
            utils.updateAppProgress(50);
            sleep(2000)
            tasks[3].click();
            sleep(5000);
            back();
            var cnt  = 1;
            while(cnt < 3){
                var finalActivity = currentActivity();
                tasks[4].click();
                toastLog("完成第 "+ cnt +"个晓食票任务")    
                sleep(5000);
                utils.waitAdvert(finalActivity);
                back();
                sleep(8000)
                cnt ++;
              }           
            }
        }
    }
   

 

function main() {
    let open_app = launchAppEnhance("链摊");
    if(open_app) {
      task()
    }
    else {
      toastLog("链摊开失败，请确认是否安装并授权打开!");
    }
  }
  
  try {
    main();
    utils.updateAppProgress(100);
  } catch (error) {
    toastLog(error);
  }
  
  storage.put("script_running_status", "end");